﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Global.Class.Library;
using DataTransferObject;
using DataAccessLayer;
using BusinessLogicLayer;
using DataAccessLayer.GCGBPlasterDTSTableAdapters;

namespace PresentationLayer
{
    public partial class WizardGCGBPlasterNew : Form
    {
        private GCGBPlasterMaster gcgbPlasterMaster;
        private int gcgbCommandID;

        private CurrencyManager gcgbCommandCurrencyManager;

        public int GCGBCommandID
        {
            get { return this.gcgbCommandID; }
            set
            {
                if (this.gcgbCommandID != value)
                {
                    this.gcgbCommandID = value;
                }
            }
        }

        GCGBPlasterWizardGetTableAdapter getWizardTableAdapter;
        protected GCGBPlasterWizardGetTableAdapter GetWizardTableAdapter
        {
            get
            {
                if (getWizardTableAdapter == null) getWizardTableAdapter = new GCGBPlasterWizardGetTableAdapter();
                return getWizardTableAdapter;
            }
        }

        public WizardGCGBPlasterNew(GCGBPlasterMaster gcgbPlasterMaster)
        {
            InitializeComponent();

            try
            {
                if (GlobalVariables.shouldRestoreProcedure) RestoreProcedure();

                this.gcgbPlasterMaster = gcgbPlasterMaster;

                this.GCGBCommandID = this.gcgbPlasterMaster.GCGBCommandID;

                GCGBPlasterDTS.GCGBPlasterWizardGetDataTable gcgbCommandDataTable = this.GetWizardTableAdapter.GetData(this.gcgbPlasterMaster.GCGBPlasterID);

                gcgbCommandDataTable.PrimaryKey = new DataColumn[] { gcgbCommandDataTable.Columns["GCGBCommandID"] };

                this.GCGBCommandID = this.gcgbPlasterMaster.GCGBCommandID;

                this.dataGridViewBG1.AutoGenerateColumns = false;
                this.dataGridViewBG1.DataSource = gcgbCommandDataTable;

                gcgbCommandCurrencyManager = (CurrencyManager)this.BindingContext[gcgbCommandDataTable];

                this.DataBindings.Add("GCGBCommandID", gcgbCommandDataTable, "GCGBCommandID");

                if (gcgbCommandDataTable.Rows.Count > 0)
                {
                    gcgbCommandCurrencyManager.Position = gcgbCommandDataTable.Rows.IndexOf(gcgbCommandDataTable.Rows.Find(this.GCGBCommandID));
                }

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }      


        private void RestoreProcedure()
        {
            string queryString;

            queryString = "     @GCGBPlasterID int " + "\r\n";
            queryString = queryString + " WITH ENCRYPTION " + "\r\n";
            queryString = queryString + " AS " + "\r\n";
            queryString = queryString + " SELECT DISTINCT GCGBCommandCollection.GCGBCommandID, GCGBCommandCollection.GCGBCommandReference, GCGBCommandCollection.GCGBCommandDate, GCGBCommandMaster.GCGBCommandTypeID, GCGBCommandMaster.EntryDate, GCGBCommandMaster.Description, GCGBCommandMaster.Remarks, GCGBCommandCollection.GCGBCommandReference + ' [' + CONVERT(char(10), GCGBCommandCollection.GCGBCommandDate, 103) + ']' AS GCGBCommandReferenceAndDate ";
            queryString = queryString + " FROM GCGBCommandCollection INNER JOIN GCGBCommandMaster ON GCGBCommandCollection.GCGBCommandID = GCGBCommandMaster.GCGBCommandID ";
            queryString = queryString + " WHERE GCGBCommandCollection.GCGBCommandID IN (SELECT GCGBCommandID FROM gcgbPlasterMaster WHERE GCGBPlasterID = @GCGBPlasterID) OR ROUND(GCGBCommandCollection.Quantity - GCGBCommandCollection.QuantityPlaster - GCGBCommandCollection.QuantityPlasterLoss, 0) > 0 ";

            SQLDatabase.CreateStoredProcedure("GCGBPlasterWizardGet", queryString);
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                this.gcgbPlasterMaster.GCGBCommandID = this.GCGBCommandID;
                this.gcgbPlasterMaster.GCGBCommandReference = this.dataGridViewBG1.CurrentRow.Cells["GCGBCommandReference"].Value.ToString();
                this.gcgbPlasterMaster.GCGBCommandDate = Convert.ToDateTime(this.dataGridViewBG1.CurrentRow.Cells["GCGBCommandDate"].Value);
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }




    }
}
